package com.androidTool;

import static android.content.Context.KEYGUARD_SERVICE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.example.androidbrowserpro.MyApplication;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.List;

public class SystemService {
    private static Activity thisActivity;

    public static void init(Activity activity){
        thisActivity = activity;
    }

    /**
     * 关机
     */
    @JavascriptInterface
    public static void shutDown() {
        Log.v("TAG", "shutDowm");
        try {
            //获得ServiceManager类
            Class ServiceManager = Class
                    .forName("android.os.ServiceManager");
            Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
            Object oRemoteService = getService.invoke(null, Context.POWER_SERVICE);
            Class cStub = Class
                    .forName("android.os.IPowerManager$Stub");
            Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
            Object oIPowerManager = asInterface.invoke(null, oRemoteService);
            Method shutdown = oIPowerManager.getClass().getMethod("shutdown", boolean.class, boolean.class);
            shutdown.invoke(oIPowerManager, false, true);
        } catch (Exception e) {
            Log.e("TAG", e.toString(), e);
        }
    }

    /**
     * 重启
     */
    public static void reBoot() {
        /*弹出重启设备菜单 */
//        PowerManager pManager = (PowerManager)thisActivity.getSystemService(Context.POWER_SERVICE); //重启到fastboot模式
//        pManager.reboot("重启");
    }


    /**
     * 根据包名强制关闭一个应用，不管前台应用还是后台进程，需要share systemuid
     * 需要权限 FORCE_STOP_PACKAGES
     * @param context
     * @param packageName
     */
    public static boolean stopApps(Context context,String packageName) {
        try {
                SystemService.killAppProcess(context,packageName);
                ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
                Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
                forceStopPackage.setAccessible(true);
                forceStopPackage.invoke(am, packageName);
//            System.out.println("TimerV force stop package "+packageName+" successful");
//            Toast.makeText(context,"force stop package success",Toast.LENGTH_SHORT);
            return true;
        }catch(Exception ex) {
            //ex.printStackTrace();
            //Toast.makeText(context,"force stop package error",Toast.LENGTH_SHORT);
        }
        return false;
    }
    public static void killAppProcess(Context context,String packageName)
    {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infoList = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infoList) {
            //进程名称
//            String processName = info.processName;
            //进程的重要程度(越低越重要)
            int importance = info.importance;
//            if (importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
//
//            }
            //该进程下的所有包名
            String[] pkgList = info.pkgList;
            for (String pkgName : pkgList) {
                //杀掉该包对应的程序
                if(pkgName.contains(packageName)){
                    am.killBackgroundProcesses(pkgName);
                }
            }
        }
    }

    /**
     * 设置休眠时间
     *
     * @param millisecond
     * @param context
     */
    public static void setScreenSleepTime(int millisecond, Context context) {
        try {
            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT,
                    millisecond);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    /**
     * @param context
     * @return
     */
    public static int getScreenSleepTime(Context context) {
        int millisecond = 0;
        try {
            millisecond = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.SCREEN_OFF_TIMEOUT);
        } catch (Exception localException) {

        }
        return millisecond;
    }

    /**
     * 唤醒手机屏幕并解锁
     */
    public static void wakeUpAndUnlock() {
//        PowerManager pm = (PowerManager)thisActivity
//                .getSystemService(Context.POWER_SERVICE);
//        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(
//                PowerManager.ACQUIRE_CAUSES_WAKEUP |
//                        PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "wakup");
//        wl.acquire(10000);
//        wl.release();
//
//        KeyguardManager keyguardManager = (KeyguardManager)thisActivity
//                .getSystemService(KEYGUARD_SERVICE);
//        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
//
//        keyguardLock.reenableKeyguard();
//        keyguardLock.disableKeyguard();
    }

    /**
     * 判断当前屏幕是否亮
     *
     * @return
     */
    public static boolean isScreenOn() {
        PowerManager pm = (PowerManager) thisActivity
                .getSystemService(Context.POWER_SERVICE);
        boolean screenOn = pm.isScreenOn();

        return screenOn;
    }

}
